package cn.junior_programmer.springsecurityjdbc.service;

import cn.junior_programmer.springsecurityjdbc.entity.LoginUser;
import cn.junior_programmer.springsecurityjdbc.entity.User;
import cn.junior_programmer.springsecurityjdbc.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author junior_programmer
 */
@Service
public class CustomUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserManager userManager;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userManager.loadByUserName(username);

        if (Objects.isNull(user)) {

            throw new UsernameNotFoundException("用户名未找到:" + username);
        }

        return new LoginUser(user);
    }
}
